package com.anysoft.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anysoft.MentorConst;
import com.anysoft.R;
import com.anysoft.bean.ListAnnouncemetsBean;
import com.anysoft.utils.PreferencesUtils;

import java.util.List;

/**
 * 公告列表适配器
 * Created by yanbin on 2017-05-3.
 */
public class AnnouncemetAdapter extends BaseAdapter {

    private Context context;
//    private String sFundCustomerNo;

    /**
     * 公告列表
     */
    private List<ListAnnouncemetsBean.DataBean> listAnnouncemets;

    public AnnouncemetAdapter(Activity context, List<ListAnnouncemetsBean.DataBean> listAnnouncemets) {
        this.context = context;
        this.listAnnouncemets = listAnnouncemets;
//        this.sFundCustomerNo = sFundCustomerNo;
    }


    @Override
    public int getCount() {
        if (listAnnouncemets == null)
            return 0;
        return listAnnouncemets.size();
    }

    @Override
    public Object getItem(int position) {
        if(listAnnouncemets==null || position>=listAnnouncemets.size()){
            return null;
        }
        return listAnnouncemets.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.list_item_announcemet, null);
            holder = new ViewHolder();
            holder.ll_announcemet_layout = (LinearLayout) convertView.findViewById(R.id.ll_announcemet_layout);
            holder.tv_announcemet_title = (TextView) convertView.findViewById(R.id.tv_announcemet_title);
            holder.tv_announcemet_content = (TextView) convertView.findViewById(R.id.tv_announcemet_content);
            holder.tv_announcemet_date = (TextView) convertView.findViewById(R.id.tv_announcemet_date);
            holder.view_line = convertView.findViewById(R.id.view_line);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String sAlreadyRead = PreferencesUtils.getString(context, MentorConst.READ_ANNOUNCEMET_ID,"");
        if(sAlreadyRead.contains(String.format("$%s$",listAnnouncemets.get(position).getId()))){
            holder.ll_announcemet_layout.setBackgroundResource(R.color.bg_gray);
        }else{
            holder.ll_announcemet_layout.setBackgroundResource(R.color.white);
        }
        holder.tv_announcemet_title.setText(listAnnouncemets.get(position).getTitle());
        holder.tv_announcemet_content.setText(listAnnouncemets.get(position).getContentText());
        holder.tv_announcemet_date.setText(listAnnouncemets.get(position).getCreateTime());
        return convertView;
    }

    //    private View viewLine;
    public static class ViewHolder {
        LinearLayout ll_announcemet_layout;
        TextView tv_announcemet_title;
        TextView tv_announcemet_content;
        TextView tv_announcemet_date;
        View view_line;
    }

}
